-- set these properties outside of the event. That way they will only be set
-- once each time the app is run, not once for each event.
property crlf : (ASCII character 13) & (ASCII character 10)
-- This is a standard header for HTML files.
property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & Â
"MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
-- Idletime is how long you want it to remain open to
-- wait for another event. Idletime is in seconds.
-- Datestamp will contain the current date. Initialize it here.
property idletime : 300 -- set to 5 minutes
property datestamp : 0
-- this bit of code outside the sdoc event is executed at launch-time
-- it is neccesary because an idle event can happen between
-- launch and the receipt of an sdoc event (and in fact often does)
set datestamp to current date
-- This is the loop for AppleEvents received by the application.
-- When you check the syntax, AppleScript will ask you to locate
-- MacHTTP and will set the correct name at that time.
on Çevent WWW½sdocÈ path_args Â
given Çclass kforÈ:http_search_args, Çclass postÈ:post_args, Çclass methÈ:method, Çclass addrÈ:client_address, Çclass userÈ:username, Çclass passÈ:password, Çclass frmuÈ:from_user, Çclass svnmÈ:server_name, Çclass svptÈ:server_port, Çclass scnmÈ:script_name, Çclass ctypÈ:content_type
-- Variables available for use:
-- http_search_args - stuff in the URL after a ?
-- post_args - stuff in the URL after a $
-- method - GET, POST, etc. Used to tell if post_args are valid
-- client_address - IP address or domain name of remote client's host
-- from_user - non-standard. e-mail address of remote user
-- username - authenticated user name
-- password - authenticated password
-- server_name - name or IP address of this server
-- server_port - TCP/IP port number being used by this server
-- script_name - URL name of this script
-- content_type - MIME content type of post_args
-- Using the "try" clause causes the "on error" routine to be run
-- if an error occurs instead of crashing.
-- If the error occurs outside the "try"/"end try" space then
-- the "on error" routine is NOT run.
try
-- save the current date and time to check later for quitting
set datestamp to current date
-- Parse the post_args data into variables for processing
-- this script only handles the post_args. Handling the other
-- variables is trivial compared to this.
set return_page to http_10_header Â
& Â
"
Email Form Results" & "Email Form Results
" & return
-- this will produce a list of "name=value" pairs
set postarglist to tokenize post_args with delimiters {"&"}
-- process each list pair in postarglist
-- store the original AppleScript text item delimiters
set oldDelim to AppleScript's text item delimiters
-- use "=" to delimit the pairs
set AppleScript's text item delimiters to {"="}
-- traverse the list and process the items
repeat with currpostarg in postarglist
set currname to first text item of currpostarg
if currname = "name" then
set username to (Decode URL (dePlus (last text item of currpostarg)))
else if currname = "address" then
set from_address to (Decode URL (dePlus (last text item of currpostarg)))
else if currname = "subject" then
set email_subject to (Decode URL (dePlus (last text item of currpostarg)))
else if currname = "message" then
set email_body to (Decode URL (dePlus (last text item of currpostarg)))
else if currname = "S" then
-- ignore it. That's the Submit button.
else
-- you have a variable who's name you don't know. Bad news!
-- create your own errMsg and errNum to pass back
-- the number 100 has no significance. I just chose it at random
error ("Unknown field in post_args: " & currname) number 100
end if
end repeat
-- restore the old AppleScript text item delimiters settings
set AppleScript's text item delimiters to oldDelim
-- NOTE: Set the "to_address" to be the address to which you want all of the mail sent.
set to_address to "jonwd@tjp.washington.edu"
set return_page to return_page Â
& "E-mail has been sent to " & to_address & return Â
& "Message generated at: " & (current date) Â
& "" & ""
set email_host to "homer.u.washington.edu"
send_message(email_host, to_address, from_address, email_subject, email_body)
-- return the page created. A return statement ends the
-- processing of the AppleEvent
return return_page
-- here is the routine to run if an error occurs
-- errMsg contains the message sent by the System
-- errNum contains the number of the error (negative for System, AE, or AS errors)
on error errMsg number errNum
-- create a page of HTML text to return
set return_page to http_10_header Â
& Â
"
Error Page" & "Error Encountered!
" & return Â
& "An error was encountered while trying to run this script." & return
set return_page to return_page Â
& "Error Message
" & return & errMsg & return Â
& "Error Number
" & return & errNum & return Â
& "Date
" & return & (current date) & return
set return_page to return_page Â
& Â
"
Please notify Jon Wiederspan at " & Â
"jonwd@tjp.washington.edu" & " of this error." & ""
-- return the page created. A return statement ends the
-- processing of the AppleEvent
return return_page
end try
end Çevent WWW½sdocÈ
-- This handler uses the TCPIP Scripting Suite to send e-mail to your mailhost.
-- Note that email_host is the name of a mail server, not just your domain name.
-- For some reason that I haven't figured out yet (from not reading the docs),
-- this requires that you send a receipt as well as a message.
on send_message(email_host, to_address, from_address, email_subject, email_body)
set crlf to (ASCII character 13) & (ASCII character 10)
set sss to (tcp connect to host email_host port 25)
try
readresponse(sss)
tcp write data "mail from: " & from_address & return Â
stream sss using ISO88591
readresponse(sss)
tcp write data "rcpt to: " & to_address & return stream sss using ISO88591
readresponse(sss)
tcp write data "data" & return stream sss using ISO88591
readresponse(sss)
tcp write data "To: " & to_address & return stream sss using ISO88591
tcp write data "Subject: " & email_subject & return stream sss using ISO88591
tcp write data email_body & crlf & return stream sss using ISO88591
tcp write data "." & return stream sss using ISO88591
readresponse(sss)
tcp close stream sss
return
on error errMsg number errNum
set return_page to http_10_header Â
& Â
"Error Page" & "Error Encountered!
" & return Â
& "An error was encountered while trying to send the mail." & return
set return_page to return_page Â
& "Error Message
" & return & errMsg & return Â
& "Error Number
" & return & errNum & return Â
& "Date
" & return & (current date) & return
set return_page to return_page Â
& Â
"
Please notify Jon Wiederspan at " & Â
"jonwd@tjp.washington.edu" & " of this error." & ""
return return_page
end try
end send_message
-- This handler is used to read the information returned from the mail server.
-- It requires the TCPIP Scripting Suite.
on readresponse(sstream)
set LF to ASCII character (10)
set continuechar to "-"
set wholemessage to ""
try
repeat until continuechar = " "
repeat until (tcp ahead characters LF stream sstream)
end repeat
set readline to (tcp read until characters LF stream sstream using ISO88591)
set scan to (scanline(readline))
set continuechar to item 2 of scan
set wholemessage to wholemessage & " " & item 3 of scan
end repeat
set errorCode to item 1 of scan as integer
if (errorCode ³ 400) then
error wholemessage number errorCode
end if
on error errMsg number errNum
set return_page to http_10_header Â
& Â
"Error Page" & "Error Encountered!
" & return Â
& "An error was encountered while trying to read the mailer response." & return
set return_page to return_page Â
& "Error Message
" & return & errMsg & return Â
& "Error Number
" & return & errNum & return Â
& "Date
" & return & (current date) & return
set return_page to return_page Â
& Â
"
Please notify Jon Wiederspan at " & Â
"jonwd@tjp.washington.edu" & " of this error." & ""
return return_page
end try
end readresponse
-- This handler is used to test for matching messages.
-- Matches lines like: 250 test... Sender ok
on scanline(lline)
return {characters 1 through 3 of lline as string, character 4 Â
of lline as string, characters 5 through end of lline as string}
end scanline
-- The idle function is run everytime the system sends an idle message.
-- If the current date is more than idletime seconds more than the last date
-- then it is time to quit.
-- The return value tells the system how long to wait before
on idle
if (current date) > (datestamp + idletime) then
quit
end if
return 5
end idle
-- This code allows you to do any clean-up that might be necessary.
-- Example: you could write the quit event time to a log to see
-- how often the applet is running.
on quit
-- do any clean-up chores here
continue quit
end quit